﻿Public Class frmProjectsModify

    Dim strNameForm As String = "Modify Project"
    Dim mID As Integer

    Public Function Initialize(ByVal inID As Integer) As Boolean
        Try
            Me.Text = strNameForm
            mID = inID

            If Not LoadForm() Then
                MsgBox("Impossible open form.", vbExclamation, Me.Text)
                Return False
            End If
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "LoadForm"

    Private Function LoadForm() As Boolean
        Try
            Dim oTProjects As New Data.TProjects
            If oTProjects.LoadData(mID) Then
                txtName.Text = oTProjects.mName
                txtDescription.Text = oTProjects.mDescription
                txtDate.Data = oTProjects.mStartDate

            Else
                Return False
            End If
            oTProjects = Nothing
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Load data", Me.Name, "LoadForm", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModify.Click
        Try
            If IsComplete() Then
                Dim oTProjects As New Data.TProjects
                If oTProjects.LoadData(mID) Then
                    oTProjects.mName = txtName.Text
                    oTProjects.mDescription = txtDescription.Text
                    oTProjects.mStartDate = txtDate.DataMySql
                    
                    If oTProjects.Modify() Then
                        Me.DialogResult = Windows.Forms.DialogResult.OK
                    Else
                        MsgBox("Impossible modify the project.", vbExclamation, Me.Text)
                        Me.DialogResult = Windows.Forms.DialogResult.Cancel
                    End If
                Else
                    MsgBox("Impossible modify the project.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "cmdModify_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtName.Text = "" Then
                MsgBox("Insert name.", vbExclamation, Me.Name)
                txtName.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub frmTableSingleInsertion_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class